#coding=utf8
'''
Created on 2016年6月11日
'''
import os
import json
import time
import sys
import copy
from DataReader import DataReader
from DataNode import DataNode
path = '../../data'
class DataProcess:
     def __init__(self):
         self.data_reader = DataReader(path)
     def get_data(self):
        while True:
            temp=self.data_reader.get_one_set_data()
            if temp == None:
                break
            yield temp
     def get_performance_data(self,groupSize):
         data=self.get_performance_data_temp(groupSize)
         re={}
         for k,v in data[0].items():
             re[k]={}
             for k2,v2 in v.items():
                 re[k][k2]=[]
         for it in data:
             for k,v in it.items():
                 for k2,v2 in v.items():
                     re[k][k2].append(v2)
         return re
     def process(self,groupSize):
         data=self.process_temp(groupSize)
         re={}
         for k,v in data[0].items():
             re[k]={}
             for k2,v2 in v.items():
                 re[k][k2]=[]
         for it in data:
             for k,v in it.items():
                 for k2,v2 in v.items():
                     re[k][k2].append(v2)
         return re
     def get_normal_data(self,groupSize):
         data=self.process_temp(groupSize)
         maxWrit=-1
         maxRead=-1
         maxSend=-1
         maxRecv=-1
         for it in data:
             for k,v in it.items():
                 if(v['disk_read_speed']>maxRead):
                     maxRead=v['disk_read_speed']
                 if(v['disk_write_speed']>maxWrit):
                     maxWrit=v['disk_write_speed']
                 if(v['net_send_speed']>maxSend):
                     maxSend=v['net_send_speed']
                 if(v['net_rec_speed']>maxRecv):
                     maxRecv=v['net_rec_speed']
         for it in data:
             for k,v in it.items():
                 v['disk_read_speed']/=(maxRead*1.0)
                 v['disk_write_speed']/=(maxWrit*1.0)
                 v['net_send_speed']/=(maxSend*1.0)
                 v['net_rec_speed']/=(maxRecv*1.0)
                 v['cpu_all_percent']/=(100*1.0)
                 v['memory_percent']/=(100*1.0)
         self.data_reader.close_all_file()
         re={}
         for k,v in data[0].items():
             re[k]={}
             for k2,v2 in v.items():
                 re[k][k2]=[]
         for it in data:
             for k,v in it.items():
                 for k2,v2 in v.items():
                     re[k][k2].append(v2)
         return re
     def get_performance_data_temp(self,groupSize):
         data=self.process_temp(groupSize)
         performance=[]
         maxWrit=-1
         maxRead=-1
         maxSend=-1
         maxRecv=-1
         for it in data:
             for k,v in it.items():
                 if(v['disk_read_speed']>maxRead):
                     maxRead=v['disk_read_speed']
                 if(v['disk_write_speed']>maxWrit):
                     maxWrit=v['disk_write_speed']
                 if(v['net_send_speed']>maxSend):
                     maxSend=v['net_send_speed']
                 if(v['net_rec_speed']>maxRecv):
                     maxRecv=v['net_rec_speed']
         for it in data:
             temp={}
             for k,v in it.items():
                 list={}
                 list['timestamp']=v['timestamp']
                 list['performance']=(v['cpu_all_percent']/100.0+v['memory_percent']/100.0+v['disk_read_speed']/(1.0*maxRead)+v['disk_write_speed']/(1.0*maxWrit)+v['net_send_speed']/(1.0*maxSend)+v['net_rec_speed']/(1.0*maxRecv))/6
                 temp[k]=list
             performance.append(temp)
         self.data_reader.close_all_file()
         return performance
     def process_temp(self,groupSize):
         data=[]
         dataAfterProcess=[]
         for row in self.get_data():
             data.append(row)
         '''
         data[0]['59.66.97.143']['ip']='a'
         print data[0]['59.66.97.143']['ip']
         print len(data[0]['59.66.97.143'])
         '''
        
         count =0;
         i=0
         while i<len(data):
             rowTemp=copy.deepcopy(data[0])
             for k,v in rowTemp.items():
                 v['cpu_all_percent']=0
                 v['memory_percent']=0
                 v['disk_read_speed']=0
                 v['disk_write_speed']=0
                 v['net_send_speed']=0
                 v['net_rec_speed']=0
             count=0
             #print data[0]['59.66.97.143']['cpu_all_percent']
             while count<groupSize:
                 for k,v in data[i].items():
                     #print v['cpu_all_percent']
                     rowTemp[k]['cpu_all_percent']+=v['cpu_all_percent']
                     rowTemp[k]['memory_percent']+=v['memory_percent']
                     rowTemp[k]['disk_read_speed']+=v['disk_read_speed']
                     rowTemp[k]['disk_write_speed']+=v['disk_write_speed']
                     rowTemp[k]['net_send_speed']+=v['net_send_speed']
                     rowTemp[k]['net_rec_speed']+=v['net_rec_speed']
                     rowTemp[k]['timestamp'] =v['timestamp']
                 count+=1
                 i+=1
                 if(i==len(data)and count<groupSize):
                     return dataAfterProcess
             for k,v in rowTemp.items():
                 v['cpu_all_percent']/=groupSize
                 v['memory_percent']/=groupSize
                 v['disk_read_speed']/=groupSize
                 v['disk_write_speed']/=groupSize
                 v['net_send_speed']/=groupSize
                 v['net_rec_speed']/=groupSize
             dataAfterProcess.append(rowTemp)
         self.data_reader.close_all_file()
         return dataAfterProcess
if __name__ == '__main__':
     dr = DataProcess()
     temp=dr.get_performance_data(1)
     print temp
     